﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace UFODefense
{
    class LaserGun : DrawableGameComponent
    {
        Vector2 m_target;
        double m_lastShotTime;
        SpriteBatch m_spriteBatch;
        ContentManager m_contentManager;
        SoundEffect m_laserS;
        Texture2D m_crosshairT;
        Laser m_laser;
        Random m_randomizer;

	    //Update ( take in mouse movement->calculate angle,take in mouse clicking->shoot missile, update timeSinceLastShot)
        public LaserGun(Game game, SpriteBatch SB, ContentManager CM, Random randomizer)
            : base(game)
        {
            m_spriteBatch = SB;
            m_contentManager = CM;
            m_randomizer = randomizer;

            game.Components.Add(this);

        }
        public override void Initialize()
        {

            //initialize lastshottime to -1 seconds so the gun can be fired right away
            m_lastShotTime = -Definitions.GUN_COOLDOWN;

            m_target.X = Mouse.GetState().X;
            m_target.Y = Mouse.GetState().Y;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            m_laserS = m_contentManager.Load<SoundEffect>("SFX/laser");
            m_crosshairT = m_contentManager.Load<Texture2D>("GFX/crosshair");

            base.LoadContent();
        }
        public override void Update(GameTime gameTime)
        {
            MouseState mouseState = Mouse.GetState();
            m_target.X = mouseState.X;
            m_target.Y = mouseState.Y;


            //if left mousebutton is pressed 
            if (mouseState.LeftButton == ButtonState.Pressed) 
            {
                //and the gun is off cooldown and the target is above the ground level
                if ((gameTime.TotalGameTime.TotalMilliseconds - m_lastShotTime > Definitions.GUN_COOLDOWN)
                    && (m_target.Y < Definitions.GROUND_Y))
                {
                    m_lastShotTime = gameTime.TotalGameTime.TotalMilliseconds;
                    m_laserS.Play();
                    //m_laser = new Laser(Game, m_spriteBatch, m_contentManager, m_randomizer, m_target);
                }
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            //calculate the rectangle for the crosshair so that it's centered on the mouse cursor
            Rectangle targetRect = new Rectangle((int)(m_target.X - (m_crosshairT.Width / 2)), 
                (int)(m_target.Y - (m_crosshairT.Height / 2)), 
                m_crosshairT.Width, m_crosshairT.Height);
            m_spriteBatch.Draw(m_crosshairT, targetRect, Color.White);

            base.Draw(gameTime);
        }
    }
}
